package com.linln.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class DateUtils {
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_PATTERN = "yyyy-MM-dd";

    public static String formatDate(Date date) {
        return format(date, DATE_PATTERN);
    }
	
	 public static String format(Date date) {
	        return format(date, DATE_TIME_PATTERN);
	    }
	 public static String format(Date date, String pattern) {
	        if (date != null) {
	            SimpleDateFormat df = new SimpleDateFormat(pattern);
	            return df.format(date);
	        }
	        return null;
	    }
	/**
	 * 根据开始时间和周期计算结束时间
	 * @param nowDate 当前时间
	 * @param day 时长   1=7天/2=半个月/3=1个月/4=1季度/5=半年/6=1年
	 */
	public static Date getDateInfo(Date nowDate,int day) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm");
	    java.util.Calendar cal = java.util.Calendar.getInstance();
	    cal.setTime(nowDate);
	    switch(day){
	    case 1 :
	       cal.add(java.util.Calendar.DATE, 7); 
	       break; //可选
	    case 2 :
	       //语句
	    	cal.add(java.util.Calendar.DATE, 15);	
	       break; //可选
	    case 3 :
	    	   cal.add(java.util.Calendar.MONTH, 1);
		       break; //可选
	    case 4 :
		       //语句
	    	   cal.add(java.util.Calendar.MONTH, 3);
		       break; //可选
	    case 5 :
		       //语句
	    	   cal.add(java.util.Calendar.MONTH, 6);
		       break; //可选
	    case 6 :
		       //语句
	    	   cal.add(java.util.Calendar.YEAR, 1);
		       break; //可选
	    //你可以有任意数量的case语句
	    default : //可选
	       //语句
	    	 
	}
	     
	    System.out.println(sdf.format(cal.getTime()));
	    return cal.getTime();
	}
	
	
	
	/**
	 * 根据开始时间和周期计算结束时间
	 * @param nowDate 当前时间
	 * @param day 时长   1=7天/2=半个月/3=1个月/4=1季度/5=半年/6=1年
	 */
	public static Date getDateHourInfo(Date nowDate,int hour) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm");
	    java.util.Calendar cal = java.util.Calendar.getInstance();
	    cal.setTime(nowDate);
	    cal.add(java.util.Calendar.MINUTE, hour); 
	    System.out.println(sdf.format(cal.getTime()));
	    return cal.getTime();
	}
	
	
	/**
	 * 根据开始时间和周期计算结束时间
	 * @param nowDate 当前时间
	 * @param day 时长   1=7天/2=半个月/3=1个月/4=1季度/5=半年/6=1年
	 */
	public static Date getDateInfoNew(Date nowDate,int day) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
	    java.util.Calendar cal = java.util.Calendar.getInstance();
	    cal.setTime(nowDate);
	    cal.add(java.util.Calendar.DATE, day); 
	    System.out.println(sdf.format(cal.getTime()));
	    return cal.getTime();
	}
	
	
	/**
	 * 根据开始时间和周期计算结束时间
	 * @param nowDate 当前时间
	 * @param day 时长   1=7天/2=半个月/3=1个月/4=1季度/5=半年/6=1年
	 */
	public static String getDateInfoString(Date nowDate,int day) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
	    java.util.Calendar cal = java.util.Calendar.getInstance();
	    cal.setTime(nowDate);
	    cal.add(java.util.Calendar.DATE, day); 
	    System.out.println(sdf.format(cal.getTime()));
	    return sdf.format(cal.getTime());
	}
	
	public static void main(String[] args) {
		Date nowDate=new Date();
		DateUtils.getDateInfo(nowDate, 1);
		DateUtils.getDateInfo(nowDate, 2);
		DateUtils.getDateInfo(nowDate, 3);
		DateUtils.getDateInfo(nowDate, 4);
		DateUtils.getDateInfo(nowDate, 5);
		DateUtils.getDateInfo(nowDate, 6);
		DateUtils.getDateInfo(nowDate, 0);

		DateUtils.getDayMap();
		
		Date a=DateUtils.getDateHourInfo(nowDate, 120);
		System.out.println("a>>>>"+a); 
	}
	
	 /**
     * 获取当前日期前30天的日期(用于返回30天内血糖和血压)
     */
    public static Map<String, String> getDayMap(){
        try {
    		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Calendar calc =Calendar.getInstance();    
            Map<String, String> bloodMap = new HashMap<String, String>();
             for(int i=1;i<=30;i++){  
                   calc.setTime(new Date());    
                   calc.add(calc.DATE, -i);    
                   Date minDate = calc.getTime();    
                   bloodMap.put(sdf.format(minDate), "0");
                   System.out.println(sdf.format(minDate)); 
                  }  
             return bloodMap;
        } catch (Exception e) {
              e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取当前日期前30天的日期(用于返回30天内血糖和血压)
     */
    public static Map<String, Integer> getDayMap1(){
        try {
    		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Calendar calc =Calendar.getInstance();    
            Map<String, Integer> bloodMap = new LinkedHashMap<String, Integer>();
             for(int i=30;i>=1;i--){  
                   calc.setTime(new Date());    
                   calc.add(calc.DATE, -i);    
                   Date minDate = calc.getTime();    
                   bloodMap.put(sdf.format(minDate), 0);
                   System.out.println(sdf.format(minDate)); 
                  }  
             return bloodMap;
        } catch (Exception e) {
              e.printStackTrace();
        }
        return null;
    }


	/**
	 *	字符串转时间
	 * @param dateStr
	 * @param format
	 * @return
	 */
    public static Date strToDate(String dateStr,String format){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			Date date = simpleDateFormat.parse(dateStr);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
